<template>
  <el-card>
    <!-- 1.0  标题-->
    <template #header>
      账号列表
    </template>
    <!-- 2.0 内容-->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      size="mini"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="account" label="账号"> </el-table-column>
      <el-table-column prop="userGroup" label="用户组"> </el-table-column>
      <el-table-column label="创建时间">
        <template v-slot="{ row }">
          {{ row.ctime | time() }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button size="mini" @click="handleEdit(row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 3.0 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 3, 4]"
      :page-size="pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 4.0 按钮 -->
    <el-button type="danger" size="mini" @click="batch">批量删除</el-button>
    <el-button type="primary" size="mini" @click="toggleSelection()"
      >取消全选</el-button
    >
    <!-- 5.0 弹出框 -->
    <el-dialog title="编辑账户信息" :visible.sync="dialogVisible" width="50%">
      <!-- 5.1 内容 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item label="ID" prop="id">
          <el-input v-model="ruleForm.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="ruleForm.account"></el-input>
        </el-form-item>
        <el-form-item label="用户组" prop="userGroup">
          <el-select v-model="ruleForm.userGroup" placeholder="请选择活动区域">
            <el-option value="普通管理员">普通管理员</el-option>
            <el-option value="超级管理员">超级管理员</el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 5.2 按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="query">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import moment from 'moment'
import {
  listUsers,
  delUsers,
  editUsers,
  batchdelUsers
} from '../../api/users.js'
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 3,
      total: 0,
      tableData: [],
      multipleSelection: [],
      dialogVisible: false,
      ruleForm: {
        id: '',
        account: '',
        userGroup: ''
      },
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        userGroup: [
          { required: true, message: '请选择管理人员', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.listUser()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.listUser()
    },
    /* 获取数据列表的接口 */
    async listUser() {
      const { data, total } = await listUsers({
        currentPage: parseInt(this.currentPage),
        pageSize: parseInt(this.pageSize)
      })
      this.tableData = data
      this.total = total
    },
    /* 删除当前行的数据 */
    handleDelete(row) {
      this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code, msg } = await delUsers({
            id: row.id
          })
          if (code === 0) {
            this.listUser()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    /* 弹出蒙层，渲染当前行的数据 */
    handleEdit(row) {
      this.dialogVisible = true
      this.ruleForm.id = row.id
      this.ruleForm.account = row.account
      this.ruleForm.userGroup = row.userGroup
    },
    /* 确定修改数据 */
    async query() {
      const { code, msg } = await editUsers({
        id: this.ruleForm.id,
        account: this.ruleForm.account,
        userGroup: this.ruleForm.userGroup
      })
      if (code === 0) {
        this.listUser()
        this.dialogVisible = false
      }
    },
    /* 批量删除 */
    batch() {
      this.$confirm('此操作将永久删除这些账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const arr = []
          this.multipleSelection.forEach(item => {
            arr.push(item.id)
          })
          const { code, msg } = await batchdelUsers({
            ids: JSON.stringify(arr)
          })
          if (code === 0) {
            this.listUser()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    /* 獲取當前id */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /* 取消全选----饿了么UI自带 */
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    }
  },
  created() {
    this.listUser()
  },
  /* 时间整理  */
  filters: {
    time(str) {
      return moment(str).format('YYYY-MM-DD  HH:mm:ss')
    }
  },
  watch: {
    total() {
      if (
        this.total === (this.currentPage - 1) * this.pageSize &&
        this.total !== 0
      ) {
        this.currentPage -= 1
        this.list()
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-pagination {
  margin: 10px 0;
}

/deep/.el-input__inner {
  width: 178px;
}
.el-pagination {
  /deep/.el-input__inner {
    width: 100px;
  }
}
</style>
